/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package cn.eclass.dao;

import cn.eclass.entity.Entity;
import cn.eclass.entity.SchoolEntity;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 *
 * @author fortruth
 */
public class SchoolDao extends CommonDao {

    private static final String TABLE_NAME = "school";

    public SchoolDao() {
        super(SchoolDao.class.getName());
    }

    @Override
    protected Entity fillEntity(ResultSet resultSet) {
        SchoolEntity entity = new SchoolEntity();
        try {
            entity.setId(Integer.valueOf(resultSet.getString("ID")));
            entity.setLink(resultSet.getString("LINK"));
            entity.setName(resultSet.getString("NAME"));
            entity.setLogo(resultSet.getString("LOGO"));
            entity.setOrderAmount(resultSet.getInt("ORDER-AMOUNT"));
        } catch (SQLException ex) {
            logger.error(ex);
        }
        return entity;
    }

    public int add(SchoolEntity entity) {
        String sql = "insert into " + TABLE_NAME
                + "(`LINK`,`NAME`,`LOGO`,`ORDER-AMOUNT`) "
                + "values ("
                + "'" + entity.getLink() + "',"
                + "'" + entity.getName() + "',"
                + "'" + entity.getLogo() + "',"
                + entity.getOrderAmount()
                + ")";
        return super.insert(sql);
    }

    public boolean update(SchoolEntity entity) {
        String sql = "update " + TABLE_NAME + " set "
                + "`LINK` = '" + entity.getLink() + "',"
                + "`NAME` = '" + entity.getName() + "',"
                + "`LOGO` = '" + entity.getLogo() + "',"
                + "`ORDER-AMOUNT` = " + entity.getOrderAmount()
                + " where id = " + entity.getId();
        int effectedRows = super.update(sql);
        if (effectedRows > 0) {
            return true;
        } else {
            return false;
        }
    }

    public boolean delete(int id) {
        String sql = "delete from " + TABLE_NAME + " where id = " + id;
        int effectedRows = super.delete(sql);
        if (effectedRows > 0) {
            return true;
        } else {
            return false;
        }
    }

    public SchoolEntity get(int id) {
        SchoolEntity entity = null;
        String sql = "select * from " + TABLE_NAME + " where id = " + id;
        List<SchoolEntity> entities = (List<SchoolEntity>) super.select(sql);
        if (entities.size() > 0) {
            entity = entities.get(0);
        }
        return entity;
    }

    public SchoolEntity getByName(String name) {
        SchoolEntity entity = null;
        String sql = "select * from " + TABLE_NAME + " where name = " + name;
        List<SchoolEntity> entities = (List<SchoolEntity>) super.select(sql);
        if (entities.size() > 0) {
            entity = entities.get(0);
        }
        return entity;
    }

    public List<SchoolEntity> list() {
        String sql = "select * from " + TABLE_NAME;
        return (List<SchoolEntity>) super.select(sql);
    }
}
